package util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.ParseException;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;

@SuppressWarnings("deprecation")
public abstract class SendGetCustomer {

    public static String URL = "http://192.168.10.21:6888/http/GetBaseCode";
    // public static String URL = "http://222.175.52.44:5888/http/GetBaseCode";



    public static void main(String[] args) throws ParseException {
    /*	String object="{\"baseCode\":\"1002\",\"resultCode\":\"没有该基地内码配置数据\",\"status\":\"0\",\"data\":{\"baseCode\":\"1002\",\"fName\":\"\",\"baseType\":\"2\"}}";
    	System.out.println(object);
    	JSONObject jsonObject=JSONObject.fromObject(object);*/

        send();
    }


    public static void  send() {

        JSONObject jsobj1 = new JSONObject();
        //基地代码
        jsobj1.put("baseCode", "1001");
        jsobj1.put("FID","");
        jsobj1.put("fName", "");
        jsobj1.put("baseType", "2");
        System.out.println(jsobj1);
        post(jsobj1);
    }

    public static String post(JSONObject json) {
        System.out.println("=========处理开始===========：");
        //System.out.println("        下发内容："+json.toString());
        System.out.println("");
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(URL);

        post.setHeader("Content-Type", "application/json");
        post.addHeader("Authorization", "Basic YWRtaW46");
        String result = "";

        try {

            StringEntity s = new StringEntity(json.toString(), "utf-8");
            s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
                    "application/json"));
            post.setEntity(s);
            // 发送请求
            HttpResponse httpResponse = client.execute(post);

            // 获取响应输入流
            InputStream inStream = httpResponse.getEntity().getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    inStream, "utf-8"));
            StringBuilder strber = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null)
                strber.append(line + "\n");
            inStream.close();
            result = strber.toString();
            System.out.println("        处理结果："+result);

            if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

                // System.out.println("请求服务器成功，做相应处理");
                JSONObject jsonObject=JSONObject.fromObject(result);
                if("0".equals(jsonObject.getString("status"))){
                    System.out.println("==============查询失败===========");
                }else{

                    System.out.println("==============查询成功===========");
                    JSONArray jsonArray=JSONArray.fromObject(jsonObject.getString("data"));
                    for (int i = 0; i < jsonArray.size(); i++) {
                        JSONObject jsonObject2=JSONObject.fromObject(jsonArray.get(i));
                        System.out.println(jsonObject2.toString());
                    }

                }

            } else {
                System.out.println("请求服务端失败");
            }
        } catch (Exception e) {
            System.out.println("请求异常");
            throw new RuntimeException(e);
        }
        System.out.println("==========处理结束：============");
        System.out.println("");
        return result;
    }

}